/**
 * Copyright (C) 2012 Oleg Anastassov <o.anastasov@gmail.com>
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kz.kstu.ent.desktop.common;

import kz.kstu.ent.app.delegate.api.ResultSheetDelegate;
import kz.kstu.ent.app.delegate.impl.ResultSheetDelegateImpl;
import kz.kstu.ent.business.entity.Exam;
import kz.kstu.ent.business.entity.ResultSheet;
import kz.kstu.ent.desktop.ui.dialog.card.ResultSheetHelper;
import kz.kstu.ent.util.DateUtil;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

import java.util.List;

/**
 * Date: 23.09.12
 * Time: 14:26
 *
 * @author Oleg Anastassov
 */
public class DatasetConverter {

    public DatasetConverter() {
    }

    public TimeSeriesCollection convertResultSheets(List<ResultSheet> sheets) {
        final TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.setDomainIsPointsInTime(true);

        final TimeSeries ts = new TimeSeries("Series 1", Day.class);
        ResultSheetHelper helper = new ResultSheetHelper();
        for (ResultSheet rs : sheets) {
            ts.add(new Day(rs.getExam().getDate()), helper.getSumAll(rs));
        }
        dataset.addSeries(ts);
        return dataset;
    }

    public TimeSeriesCollection convertExams(List<Exam> exams) {
        ResultSheetDelegate delegate = new ResultSheetDelegateImpl();

        final TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.setDomainIsPointsInTime(true);

        ResultSheetHelper helper = new ResultSheetHelper();
        final TimeSeries ts = new TimeSeries("Series 1", Day.class);
        for (Exam ex : exams) {
            List<ResultSheet> sheets = delegate.findByExamId(ex.getId());
            ts.add(new Day(ex.getDate()), helper.getAverage(sheets));
        }
        dataset.addSeries(ts);
        return dataset;
    }

    public DefaultCategoryDataset convertExamsForBarChart(List<Exam> exams) {
        final String AVERAGE_MARK = "Средний балл";

        ResultSheetDelegate delegate = new ResultSheetDelegateImpl();
        ResultSheetHelper helper = new ResultSheetHelper();

        DefaultCategoryDataset ds = new DefaultCategoryDataset();
        for (Exam exam : exams) {
            List<ResultSheet> sheets = delegate.findByExamId(exam.getId());
            Double average = helper.getAverage(sheets);
            ds.setValue(average, AVERAGE_MARK, DateUtil.formatDate(exam.getDate(), DateUtil.SIMPLE_DATE_PATTERN));
        }
        return ds;
    }

    public DefaultCategoryDataset convertMathResults(List<Exam> exams) {
        final String AVERAGE_MARK = "Средний балл";

        ResultSheetDelegate delegate = new ResultSheetDelegateImpl();
        ResultSheetHelper helper = new ResultSheetHelper();

        DefaultCategoryDataset ds = new DefaultCategoryDataset();
        for (Exam exam : exams) {
            List<ResultSheet> sheets = delegate.findByExamId(exam.getId());
            Double average = helper.getAverageMath(sheets);
            ds.setValue(average, AVERAGE_MARK, DateUtil.formatDate(exam.getDate(), DateUtil.SIMPLE_DATE_PATTERN));
        }
        return ds;
    }

    public DefaultCategoryDataset convertRussianResults(List<Exam> exams) {
        final String AVERAGE_MARK = "Средний балл";

        ResultSheetDelegate delegate = new ResultSheetDelegateImpl();
        ResultSheetHelper helper = new ResultSheetHelper();

        DefaultCategoryDataset ds = new DefaultCategoryDataset();
        for (Exam exam : exams) {
            List<ResultSheet> sheets = delegate.findByExamId(exam.getId());
            Double average = helper.getAverageRussian(sheets);
            ds.setValue(average, AVERAGE_MARK, DateUtil.formatDate(exam.getDate(), DateUtil.SIMPLE_DATE_PATTERN));
        }
        return ds;
    }

    public DefaultCategoryDataset convertKazakhResults(List<Exam> exams) {
        final String AVERAGE_MARK = "Средний балл";

        ResultSheetDelegate delegate = new ResultSheetDelegateImpl();
        ResultSheetHelper helper = new ResultSheetHelper();

        DefaultCategoryDataset ds = new DefaultCategoryDataset();
        for (Exam exam : exams) {
            List<ResultSheet> sheets = delegate.findByExamId(exam.getId());
            Double average = helper.getAverageKazakh(sheets);
            ds.setValue(average, AVERAGE_MARK, DateUtil.formatDate(exam.getDate(), DateUtil.SIMPLE_DATE_PATTERN));
        }
        return ds;
    }

    public DefaultCategoryDataset convertKazakhHistoryResults(List<Exam> exams) {
        final String AVERAGE_MARK = "Средний балл";

        ResultSheetDelegate delegate = new ResultSheetDelegateImpl();
        ResultSheetHelper helper = new ResultSheetHelper();

        DefaultCategoryDataset ds = new DefaultCategoryDataset();
        for (Exam exam : exams) {
            List<ResultSheet> sheets = delegate.findByExamId(exam.getId());
            Double average = helper.getAverageKazakhHistory(sheets);
            ds.setValue(average, AVERAGE_MARK, DateUtil.formatDate(exam.getDate(), DateUtil.SIMPLE_DATE_PATTERN));
        }
        return ds;
    }

}
